package com.syq.play.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

import com.syq.play.AppContent;
import com.syq.play.R;
import com.syq.play.adapter.FolderGridAdapter;
import com.syq.play.adapter.IndexAdapter;
import com.syq.play.api.MediaStoreApi;
import com.syq.play.model.MediaModel;
import com.syq.play.ui.IndexActivity;
import com.syq.play.util.MyStringUtil;
/**
 * 获取音频数据实现类
 * @author ShiYiqiong
 *
 */
public class MediaStoreImpl implements MediaStoreApi{

	public List<MediaModel> getAllMedia() {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<HashMap<String, String>> getAllAlbum() {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<HashMap<String, String>> getAllArtist() {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<HashMap<String, String>> getAllFolder() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MediaModel> getMediaFromAlbum(String mediaAlbum) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MediaModel> getMediaFromArtist(String mediaArtist) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MediaModel> getMediaFromFolder(String FolderName) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<HashMap<String, Object>> getHomePageData() {
		// TODO Auto-generated method stub
		return null;
	}
	/*private static final String TAG="MediaStoreImpl";
	private Context context;
	private static final String MIN_TIME="10000";
	
	*//**
	 * 采用单例模式，封装构造方法
	 *//*
	private MediaStoreImpl(){
	}
	*//**
	 * 设置context
	 * @param context
	 *//*
	private void setContext(Context context){
		this.context=context;
	}
	*//**
	 * 获取所有多媒体文件
	 *//*
	public List<MediaModel> getAllMedia() {
		if(AppContent.allMedia==null || AppContent.allMedia.isEmpty()){
			List<MediaModel> result=null;
			String selection=MediaStore.Audio.Media.DURATION+">=?";
			String[] selectionArgs=new String[] { MIN_TIME };
			ContentResolver cr=context.getContentResolver();
			Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			if(cursor.moveToFirst()){
				result=new ArrayList<MediaModel>();
				while(cursor.moveToNext()){
					int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
					String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
					String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
					String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
					String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
					int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
					long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
					MediaModel data=new MediaModel();
					data.setMediaId(id);
					data.setMediaTitle(title);
					data.setMediaAlbum(album);
					data.setMediaArtist(artist);
					data.setMediaData(url);
					data.setMediaDuration(MyStringUtil.formatLongToTimeStr((long)duration));
					data.setMediaSize(size);
					result.add(data);
				}
			}
			if(!cursor.isClosed()){
				cursor.close();
			}
			Log.i(TAG, TAG+":"+result);
			AppContent.allMedia=result;
			}	
		return AppContent.allMedia;
	}
	*//**
	 * 获取所有专辑数据
	 *//*
	public Set<HashMap<String, String>> getAllAlbum() {
		if(AppContent.allAlbum==null || AppContent.allAlbum.isEmpty()){
			Set<String> result=null;
			String selection=MediaStore.Audio.Media.DURATION+">=?";
			String[] selectionArgs=new String[] { MIN_TIME };
			ContentResolver cr=context.getContentResolver();
			Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ "distinct "+MediaStore.Audio.Media.ALBUM }, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			if(cursor.moveToFirst()){
				result=new HashSet<String>();
				do{
					String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
					result.add(album);
				}while(cursor.moveToNext());
			}
			if(!cursor.isClosed()){
				cursor.close();
			}
			if(result!=null){
				for(String title : result){
					if(AppContent.allAlbum==null){
						AppContent.allAlbum=new HashSet<HashMap<String,String>>();
					}
					HashMap<String,String> item=new HashMap<String, String>();
					item.put(FolderListAdapter.FOLDER_TITLE, title);
					item.put(FolderListAdapter.FILE_NUM, ""+getMediaFromAlbum(title).size());
					AppContent.allAlbum.add(item);
				}
			}
		}
		return AppContent.allAlbum;
	}
	*//**
	 * 获取所有歌手数据
	 *//*
	public Set<HashMap<String, String>> getAllArtist() {
		if(AppContent.allArtist==null || AppContent.allArtist.isEmpty()){
			Set<String> result=null;
			String selection=MediaStore.Audio.Media.DURATION+">=?";
			String[] selectionArgs=new String[] { MIN_TIME };
			ContentResolver cr=context.getContentResolver();
			Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ "distinct "+MediaStore.Audio.Media.ARTIST }, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			if(cursor.moveToFirst()){
				result=new HashSet<String>();
				do{
					String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
					result.add(artist);
				}while(cursor.moveToNext());
			}
			if(!cursor.isClosed()){
				cursor.close();
			}
			if(result!=null){
				for(String title : result){
					if(AppContent.allArtist==null){
						AppContent.allArtist=new HashSet<HashMap<String,String>>();
					}
					HashMap<String,String> item=new HashMap<String, String>();
					item.put(FolderListAdapter.FOLDER_TITLE, title);
					item.put(FolderListAdapter.FILE_NUM, ""+getMediaFromArtist(title).size());
					AppContent.allArtist.add(item);
				}
			}
		}
		return AppContent.allArtist;
	}
	*//**
	 * 获取所有文件夹数据
	 *//*
	public Set<HashMap<String, String>> getAllFolder() {
		if(AppContent.allFolder==null || AppContent.allFolder.isEmpty()){
			Set<String> set=null;
			String selection=MediaStore.Audio.Media.DURATION+">=?";
			String[] selectionArgs=new String[] { MIN_TIME };
			ContentResolver cr=context.getContentResolver();
			Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ MediaStore.Audio.Media.DATA }, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			if(cursor.moveToFirst()){
				set=new HashSet<String>();
				do{
					String folder = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
					folder=folder.substring(0, folder.lastIndexOf("/"));
					set.add(folder);
				}while(cursor.moveToNext());
			}
			if(!cursor.isClosed()){
				cursor.close();
			}
			if(set!=null){
				for(String title : set){
					if(AppContent.allFolder==null){
						AppContent.allFolder=new HashSet<HashMap<String,String>>();
					}
					HashMap<String,String> item=new HashMap<String, String>();
					item.put(FolderListAdapter.FOLDER_TITLE, title);
					item.put(FolderListAdapter.FILE_NUM, ""+getMediaFromFolder(title).size());
					AppContent.allFolder.add(item);
				}
			}
		}
		return AppContent.allFolder;
	}
	*//**
	 * 根据专辑名获取所有多媒体数据
	 *//*
	public List<MediaModel> getMediaFromAlbum(String mediaAlbum) {
		List<MediaModel> result=null;
		ContentResolver cr=context.getContentResolver();
		String selection=MediaStore.Audio.Media.ALBUM+" = ? ";
		String[] selectionArgs=new String[]{mediaAlbum};
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if(cursor.moveToFirst()){
			result=new ArrayList<MediaModel>();
			do{
				int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
				String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
				String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
				String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
				String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
				long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
				MediaModel data=new MediaModel();
				data.setMediaId(id);
				data.setMediaTitle(title);
				data.setMediaAlbum(album);
				data.setMediaArtist(artist);
				data.setMediaData(url);
				data.setMediaDuration(MyStringUtil.formatLongToTimeStr((long)duration));
				data.setMediaSize(size);
				result.add(data);
			}while(cursor.moveToNext());
		}
		if(!cursor.isClosed()){
			cursor.close();
		}
		return result;
	}
	*//**
	 * 根据歌手名获取所有多媒体数据
	 *//*
	public List<MediaModel> getMediaFromArtist(String mediaArtist) {
		List<MediaModel> result=null;
		ContentResolver cr=context.getContentResolver();
		String selection=MediaStore.Audio.Media.ARTIST+" = ? ";
		String[] selectionArgs=new String[]{mediaArtist};
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if(cursor.moveToFirst()){
			result=new ArrayList<MediaModel>();
			do{
				int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
				String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
				String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
				String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
				String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
				long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
				MediaModel data=new MediaModel();
				data.setMediaId(id);
				data.setMediaTitle(title);
				data.setMediaAlbum(album);
				data.setMediaArtist(artist);
				data.setMediaData(url);
				data.setMediaDuration(MyStringUtil.formatLongToTimeStr((long)duration));
				data.setMediaSize(size);
				result.add(data);
			}while(cursor.moveToNext());
		}
		if(!cursor.isClosed()){
			cursor.close();
		}
		return result;
	}
	*//**
	 * 根据文件夹名获取所有多媒体数据
	 *//*
	public List<MediaModel> getMediaFromFolder(String FolderName) {
		List<MediaModel> result=null;
		ContentResolver cr=context.getContentResolver();
		String selection=MediaStore.Audio.Media.DATA+" LIKE ? ";
		String[] selectionArgs=new String[]{FolderName+"%"};
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if(cursor.moveToFirst()){
			result=new ArrayList<MediaModel>();
			do{
				int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
				String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
				String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
				String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
				String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
				long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
				MediaModel data=new MediaModel();
				data.setMediaId(id);
				data.setMediaTitle(title);
				data.setMediaAlbum(album);
				data.setMediaArtist(artist);
				data.setMediaData(url);
				data.setMediaDuration(MyStringUtil.formatLongToTimeStr((long)duration));
				data.setMediaSize(size);
				result.add(data);
			}while(cursor.moveToNext());
		}
		if(!cursor.isClosed()){
			cursor.close();
		}
		return result;
	}
	*//**
	 * 获取首页数据
	 *//*
	public List<HashMap<String, Object>> getHomePageData() {
//		if(AppContent.homePageData==null || AppContent.homePageData.isEmpty()){
//			List<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
//			HashMap<String, Object> item=new HashMap<String, Object>();
//			item.put(IndexAdapter.ICON_ID, R.drawable.list_icon_media);
//			item.put(IndexAdapter.TITLE, "全部歌曲");
//			List<MediaModel> allMedia=AppContent.mediaStore.getAllMedia();
//			if(allMedia!=null){
//				item.put(IndexAdapter.NUM, allMedia.size()+"首歌曲");
//			}else{
//				item.put(IndexAdapter.NUM, 0+"首歌曲");
//			}
//			item.put(IndexAdapter.FOLDER_ID, IndexActivity.ALL_MEDIA);
//			list.add(item);
//			item=new HashMap<String, Object>();
//			item.put(IndexAdapter.ICON_ID, R.drawable.list_icon_artist);
//			item.put(IndexAdapter.TITLE, "艺术家");
//			Set<HashMap<String, String>> allArtist=AppContent.mediaStore.getAllArtist();
//			if(allArtist!=null){
//				item.put(IndexAdapter.NUM, allArtist.size()+"位艺术家");
//			}else{
//				item.put(IndexAdapter.NUM, 0+"位艺术家");
//			}
//			item.put(IndexAdapter.FOLDER_ID, IndexActivity.All_ARLIST);
//			list.add(item);
//			item=new HashMap<String, Object>();
//			item.put(IndexAdapter.ICON_ID, R.drawable.list_icon_album);
//			item.put(IndexAdapter.TITLE, "专辑");
//			Set<HashMap<String, String>> allAlbum=AppContent.mediaStore.getAllAlbum();
//			if(allAlbum!=null){
//				item.put(IndexAdapter.NUM,allAlbum.size()+"张专辑");
//			}else{
//				item.put(IndexAdapter.NUM, 0+"张专辑");
//			}
//			item.put(IndexAdapter.FOLDER_ID, IndexActivity.ALL_ALBUM);
//			list.add(item);
//			item=new HashMap<String, Object>();
//			item.put(IndexAdapter.ICON_ID, R.drawable.list_icon_folder);
//			item.put(IndexAdapter.TITLE, "文件夹");
//			Set<HashMap<String, String>> allFolder=AppContent.mediaStore.getAllFolder();
//			if(allFolder!=null){
//				item.put(IndexAdapter.NUM, allFolder.size()+"个文件夹");
//			}else{
//				item.put(IndexAdapter.NUM, 0+"个文件夹");
//			}
//			item.put(IndexAdapter.FOLDER_ID, IndexActivity.ALL_FOLDER);
//			list.add(item); 
//			item=new HashMap<String, Object>();
//			item.put(IndexAdapter.ICON_ID, R.drawable.list_icon_favourite);
//			item.put(IndexAdapter.TITLE, "我的最爱");
//			item.put(IndexAdapter.NUM, 0+"首歌曲");
//			item.put(IndexAdapter.FOLDER_ID, IndexActivity.ALL_FAVOURITE_MEDIA);
//			list.add(item);
//			item=new HashMap<String, Object>();
//			item.put(IndexAdapter.ICON_ID, R.drawable.list_icon_recent);
//			item.put(IndexAdapter.TITLE, "最近播放");
//			item.put(IndexAdapter.NUM, 0+"首歌曲");
//			item.put(IndexAdapter.FOLDER_ID, IndexActivity.ALL_RECENT);
//			list.add(item);
//			AppContent.homePageData=list;
//		}
		return AppContent.homePageData;
	}
	*//**
	 * 类级内部类，在首次被访问时才会被初始化
	 *//*
	private static class SingleHolder {
		static void setContext(Context context) {
			objsingle.setContext(context);
		}

		*//**
		 * 静态初始化器，有JVM来保证线程安全
		 *//*
		private static MediaStoreImpl objsingle = new MediaStoreImpl();
	}

	*//**
	 * 单例，获得接口实现
	 * 
	 * @return
	 *//*
	public static MediaStoreImpl getInstance(Context context) {
		SingleHolder.setContext(context);
		return SingleHolder.objsingle;
	}*/
}
